/**
 * 列表
 */
var pageCurr;
$(function() {
    //layui.form.render('select','myDiv');
    layui.use('table', function(){
        var table = layui.table
            ,form = layui.form;
        tableIns=table.render({
            elem: '#restLogList'
            ,url:'/restLog/getRestLogs'
        	,method: 'post' //默认：get请求
            ,cellMinWidth: 80
            ,page: true,
            request: {
                pageName: 'page' //页码的参数名称，默认：page
                ,limitName: 'limit' //每页数据量的参数名，默认：limit
            },response:{
                statusName: 'code' //数据状态的字段名称，默认：code
                ,statusCode: 200 //成功的状态码，默认：0
                ,countName: 'totals' //数据总数的字段名称，默认：count
                ,dataName: 'list' //数据列表的字段名称，默认：data
            }
            ,cols: [[
                {type:'numbers'}
                ,{field:'createDate', title:'创建时间', width:180, unresize: true, sort: true,align:'center'}
                ,{field:'url', width:180, title:'url',align:'center'}
                ,{field:'service', width:180, title:'service',align:'center'}
                /*,{field:'isDel', title:'请求数据',width:120,align:'center',templet:'#isDelTpl'}*/
                ,{field:'times', width:80, title: '用时',align:'center'}
                /* ,{field:'updateTime', width:180, title: '返回内容',align:'center'}*/
                ,{field:'success', width:180, title: '是否成功',align:'center',templet:'#isSuccessTpl'}
                ,{field:'iIp', width:180, title: '来源ip',align:'center'}
                ,{field:'tIp', width:180, title: '服务器ip',align:'center'}
                ,{field:'appuser', width:180, title: '用户',align:'center'}
                ,{field:'appVersion', width:180, title: 'app版本',align:'center'}
                ,{field:'osVersion', width:180, title: '系统版本',align:'center'}
                ,{field:'diviceVersion', width:180, title: '手机型号',align:'center'}
                ,{field:'diviceId', width:180, title: '设备id',align:'center'}
                ,{fixed:'right', title:'操作', width:280,align:'center', toolbar:'#optBar'}
            ]]
            ,  done: function(res, curr, count){
            }
        });

        //监听工具条
        table.on('tool(restLogTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                delrestLog(data,data.kid,data.id);
            } else if(obj.event === 'showReqData'){
                //请求数据
                showLogs('showReqData',data.kid);
            } else if(obj.event === 'showContent'){
                //返回内容
                showLogs('showContent',data.kid);
            }
        });
        //监听提交
        form.on('submit(close)', function(data){
            // TODO 校验
            close(data);
            return false;
        });

    });
    //搜索框
    layui.use(['form','laydate'], function(){
        var form = layui.form ,layer = layui.layer
            ,laydate = layui.laydate;
        //日期
        laydate.render({
            elem: '#createDateStart'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#createDateEnd'
            ,type: 'datetime'
        });
        //TODO 数据校验
        //监听搜索框
        form.on('submit(searchSubmit)', function(data){
            //重新加载table
            load(data);
            return false;
        });
    });
});
//导出
function exportRestLog(){
    window.location.href="/restLog/exportRestLog"
         +"?createDateStart="+$("#createDateStart").val()
         +"&success="+$("#success").val()
         +"&appuser="+$("#appuser").val()
         +"&service="+$("#service").val();
}

//导出用户设备版本
function exportUserDevice(){
    window.location.href="/restLog/exportUserDevice"
        +"?createDateStart="+$("#createDateStart").val()
        +"&success="+$("#success").val()
        +"&appuser="+$("#appuser").val()
        +"&service="+$("#service").val();
}

//关闭弹窗
function close(obj){
    layer.closeAll();
    $("#logContent").val("");
    //加载页面
    load(obj);
}

function openRestLog(id,title){
    layer.open({
        type:1,
        title: title,
        fixed:false,
        resize :false,
        shadeClose: true,
        area: ['550px'],
        content:$('#showLog'),
        end:function(){
            $("#logContent").val("");
        }
    });
}

function showLogs(logType,id) {
    //回显数据
    $.get("/restLog/showLogs",{"id":id,"logType":logType},function(data){
        if(isLogin(data)){
            if(data.msg=="ok" && data.logContent!=null){
                $("#logContent").val(data.logContent==null?'':data.logContent);
                if("showContent"==logType){
                    openRestLog(id,"查看返回内容日志");
                }else{
                    openRestLog(id,"查看请求数据日志");
                }
            }else{
                //弹出错误提示
                layer.alert(data.msg,function () {
                    layer.closeAll();
                });
            }
        }
    });
}

function delrestLog(obj,id,name) {
    if(null!=id){
    	 layer.confirm('您确定要删除'+name+'吗？', {
	            btn: ['确认','返回'] //按钮
	        }, function(){
	            $.post("/restLog/delRestLog",{"id":id},function(data){
	            	if(isLogin(data)){
		                if(data=="ok"){
		                    //回调弹框
		                    layer.alert("删除成功！",function(){
		                        layer.closeAll();
		                        //加载load方法
		                        load(obj);//自定义
		                    });
		                }else{
		                	layer.alert(data,function(){
		                    	layer.closeAll();
		                    	//加载load方法
		                    	load(obj);//自定义
		                    });
		                }
	            	}
	            });
	        }, function(){
	            layer.closeAll();
	        });
    }
}
function load(obj){
    //重新加载table
    tableIns.reload({
        where: obj.field
        , page: {
            curr: pageCurr //从当前页码开始
        }
    });
}

